package com.bdk.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.bdk.conmon.ResultCode;
import com.bdk.conmon.ResultEntity;
import com.bdk.conmon.SystemCons;
import com.bdk.dao.MenuMapper;
import com.bdk.dao.RoleMapper;
import com.bdk.mapper.Menu;
import com.bdk.mapper.Role;
import com.bdk.mapper.User;
import com.bdk.model.MenuVO;
import com.bdk.model.PageJson;
import com.bdk.model.RoleVO;
import com.bdk.service.IMenuService;
import com.bdk.service.IRoleService;
import com.bdk.utill.JSONUtils;
import com.bdk.utill.StringHelper;

/**
 * 用户相关实现 ClassName: UserServiceImpl
 * 
 * @Description:
 * @author ChenQuan
 * @date 2017年5月4日下午1:57:23
 */
@Service("menuService")
public class MenuServiceImpl implements IMenuService {
	final Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private MenuMapper menuDao;

//1、
	@Override
	public String getMenuJson(MenuVO menuVO) {
		PageJson pageJson=new PageJson();
		List<Menu> ulist=new ArrayList<Menu>();
		try {
			menuVO.setPage(menuVO.getPage()-1);
			   ulist=  menuDao.queryMenuListPages(menuVO);
			   pageJson.setMsg("成功");
			   pageJson.setData(ulist);
			   pageJson.setCount(menuDao.queryTotal(menuVO.getManuName()));//总数据
		} catch (Exception exception) {
			// TODO: handle exception
			pageJson.setCode(111111);
			pageJson.setMsg(exception.getMessage());
		}
		
		 logger.info(JSON.toJSONString(pageJson));
		return  JSON.toJSONString(pageJson);
	}



	//2、获取角色对象
	@Override
	public Menu getMenu(MenuVO menuVO) {
		return menuDao.findbyId(menuVO);
	}



	//3、添加角色
	@Override
	public String addMenuJson(MenuVO menuVO,HttpServletRequest request) {
		  Menu menu=new Menu();
		  PageJson p=new PageJson();
		  BeanUtils.copyProperties(menuVO, menu);
		  menu.setMenuId(UUID.randomUUID().toString());//生成不重复的UUID
		  //校验用户是否已登录
		  if(SystemCons.checkLoging(request)){
			  try{
				  //设置添加用户默认所属公司
				  menuDao.insertSelective(menu);  
				  p.setCode(ResultCode.OK.getValue());
				  p.setMsg(ResultCode.OK.getName());
				  logger.info("新增用户成功");
			  }catch(Exception e){
					p.setCode(ResultCode.ERRO.getValue());
					p.setMsg(ResultCode.ERRO.getName());
					logger.info("异常信息："+e.getMessage());
			  }
		  }else{
				p.setCode(ResultCode.NOTLOGIN.getValue());
				p.setMsg(ResultCode.NOTLOGIN.getName());
				logger.info("新增用户  -- 用户未登录");
		  }
		  return JSON.toJSONString(p);
	}
	
	//4、更新角色
	public String updateMenu(MenuVO menuVO,HttpServletRequest request){
		  PageJson p=new PageJson();
		  Menu menu=new Menu();
		  BeanUtils.copyProperties(menuVO, menu);
		  if(SystemCons.checkLoging(request)){
			  try{
				  menuDao.updateByPrimaryKeySelective(menu);
			       logger.info("修改菜单信息成功");
				   p.setCode(ResultCode.OK.getValue());
				   p.setMsg(ResultCode.OK.getName());
			  }catch(Exception e){
				    p.setCode(ResultCode.ERRO.getValue());
					p.setMsg(ResultCode.ERRO.getName());
					logger.info("异常信息："+e.getMessage());
					return JSON.toJSONString(p);
			  }
		  }else{
			    p.setCode(ResultCode.NOTLOGIN.getValue());
				p.setMsg(ResultCode.NOTLOGIN.getName());
				logger.info("新增角色  -- 用户未登录");
		  }
		  return JSON.toJSONString(p);
	}
	
	//6、删除角色
	public String delMenuJson(String ids){
		PageJson pageJson=new PageJson();
		try {
			   menuDao.deleteByPrimaryKey(ids);
			   pageJson.setMsg("成功");
		} catch (Exception exception) {
			// TODO: handle exception
			pageJson.setCode(111111);
			pageJson.setMsg(exception.getMessage());
		}
		 logger.info(JSON.toJSONString(pageJson));
		return  JSON.toJSONString(pageJson);
	}
	
}
